﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace QuanLySuKien
{
    public partial class QuanLyDiaDiem : System.Web.UI.Page
    {
        string strCon = "Data Source=192.168.1.100;Initial Catalog=quanlysukien;User ID=sa;Password=123456";
        DataTable table = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        void LoadData()
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("Select * from DiaDiem", strCon);
                adapter.Fill(table);
                GridView1.DataSource = table;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool success = false;
            int maks, makv, succhua, trangthai;
            Decimal gia;
            string TenDD = txtTenDD.Text.ToString();
            string DiaChi = txtDiaChi.Text.ToString();
            string SucChua = txtSucChua.Text.ToString();
            string LienHe = txtLienHe.Text.ToString();
            string MaDD = txtMaDD.Text.ToString();
            string MaKV = txtMaKV.Text.ToString();
            string TrangThai = txtTrangThai.Text.ToString();
            string Gia = txtGia.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();
            //makv = Convert.ToInt32(MaKV);
            succhua = Convert.ToInt32(SucChua);
            gia = Convert.ToDecimal(Gia);
            trangthai=Convert.ToInt32(TrangThai);
            try
            {
                string sql = "INSERT INTO DiaDiem (TenDiaDiem, MaKV, DiaChi, LienHe, SucChua, Gia, Mota, TrangThai) " + "VALUES (@prmTenDiaDiem, @prmMaKV, @prmDiaChi, @prmLienHe, @prmSucChua, @prmGia, @prmMota, @prmTrangThai)";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmTenDiaDiem", SqlDbType.NVarChar, 100).Value = TenDD;
                    command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 200).Value = MoTa;
                    command.Parameters.Add("@prmDiaChi", SqlDbType.NVarChar, 100).Value = DiaChi;
                    command.Parameters.Add("@prmLienHe", SqlDbType.NVarChar, 100).Value = LienHe;
                    command.Parameters.Add("@prmGia", SqlDbType.Money).Value = gia;
                    command.Parameters.Add("@prmSucChua", SqlDbType.Int).Value = succhua;
                    command.Parameters.Add("@prmMaKV", SqlDbType.VarChar,10).Value = MaKV;
                    command.Parameters.Add("@prmTrangThai", SqlDbType.Int).Value = trangthai;

                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Địa Điểm đã được cập nhật.";
                    txtDiaChi.Text = "";
                    txtGia.Text = "";
                    txtLienHe.Text = "";
                    txtMaDD.Text = "";
                    txtMaKV.Text = "";
                    txtMoTa.Text = "";
                    txtSucChua.Text = "";
                    txtTenDD.Text = "";
                    txtTrangThai.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Địa Điểm chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }
        internal sealed class ConnectionManager
        {
            public static SqlConnection GetConnection()
            {
                string connectionString = "Data Source=192.168.1.100;Initial Catalog=quanlysukien;User ID=sa;Password=123456";
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
                return connection;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool success = false;
            int madd, makv, succhua, trangthai;
            Decimal gia;
            string TenDD = txtTenDD.Text.ToString();
            string DiaChi = txtDiaChi.Text.ToString();
            string SucChua = txtSucChua.Text.ToString();
            string LienHe = txtLienHe.Text.ToString();
            string MaDD = txtMaDD.Text.ToString();
            string MaKV = txtMaKV.Text.ToString();
            string TrangThai = txtTrangThai.Text.ToString();
            string Gia = txtGia.Text.ToString();
            string MoTa = txtMoTa.Text.ToString();
            madd = Convert.ToInt32(MaDD);
            //makv = Convert.ToInt32(MaKV);
            succhua = Convert.ToInt32(SucChua);
            gia = Convert.ToDecimal(Gia);
            trangthai = Convert.ToInt32(TrangThai);
            try
            {
                string sql = "UPDATE DiaDiem SET TenDiaDiem=@prmTenDiaDiem, MaKV=@prmMaKV, DiaChi=@prmDiaChi, LienHe=@prmLienHe, SucChua=@prmSucChua, Gia=@prmGia, Mota=@prmMota, TrangThai=@prmTrangThai WHERE MaDD=@prmMaDD";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmTenDiaDiem", SqlDbType.NVarChar, 100).Value = TenDD;
                    command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 200).Value = MoTa;
                    command.Parameters.Add("@prmDiaChi", SqlDbType.NVarChar, 100).Value = DiaChi;
                    command.Parameters.Add("@prmLienHe", SqlDbType.NVarChar, 100).Value = LienHe;
                    command.Parameters.Add("@prmGia", SqlDbType.Money).Value = gia;
                    command.Parameters.Add("@prmSucChua", SqlDbType.Int).Value = succhua;
                    command.Parameters.Add("@prmMaKV", SqlDbType.VarChar,10).Value = MaKV;
                    command.Parameters.Add("@prmMaDD", SqlDbType.Int).Value = madd;
                    command.Parameters.Add("@prmTrangThai", SqlDbType.Int).Value = trangthai;

                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Địa Điểm đã được cập nhật.";
                    txtDiaChi.Text = "";
                    txtGia.Text = "";
                    txtLienHe.Text = "";
                    txtMaDD.Text = "";
                    txtMaKV.Text = "";
                    txtMoTa.Text = "";
                    txtSucChua.Text = "";
                    txtTenDD.Text = "";
                    txtTrangThai.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Địa Điểm chưa thể cập nhật";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            bool success = false;
            int madd;
            string MaDD = txtMaDD.Text.ToString();
            madd = Convert.ToInt32(MaDD);
            //string ThanhPho = txtThanhPho.Text.ToString();
            //string MoTa = txtMoTa.Text.ToString();

            try
            {
                string sql = "DELETE FROM DiaDiem WHERE madd=@prmMaDD";
                using (SqlCommand command = new SqlCommand(sql, ConnectionManager.GetConnection()))
                {
                    command.Parameters.Add("@prmMaDD", SqlDbType.Int).Value = madd;
                    //command.Parameters.Add("@prmMota", SqlDbType.NVarChar, 200).Value = MoTa;
                    //command.Parameters.Add("@prmThanhPho", SqlDbType.NVarChar, 100).Value = ThanhPho;

                    int rowAffected = command.ExecuteNonQuery();
                    success = (rowAffected == 1);

                }
                if (success)
                {
                    StatusLabel.Text = "Địa Điểm đã xóa thành công";
                    txtDiaChi.Text = "";
                    txtGia.Text = "";
                    txtLienHe.Text = "";
                    txtMaDD.Text = "";
                    txtMaKV.Text = "";
                    txtMoTa.Text = "";
                    txtSucChua.Text = "";
                    txtTenDD.Text = "";
                    txtTrangThai.Text = "";
                    LoadData();
                }
                else
                {
                    StatusLabel.Text = "Địa Điểm chưa thể xóa";
                }
            }
            catch (SqlException ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtGia.Text = "";
            txtLienHe.Text = "";
            txtMaDD.Text = "";
            txtMaKV.Text = "";
            txtMoTa.Text = "";
            txtSucChua.Text = "";
            txtTenDD.Text = "";
            txtTrangThai.Text = "";
            StatusLabel.Text = "";
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int row = GridView1.SelectedIndex;
            txtMaDD .Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[0].Text);
            txtTenDD.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[1].Text);
            txtMaKV.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[2].Text);
            txtDiaChi.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[3].Text);
            txtLienHe.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[4].Text);
            txtSucChua.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[5].Text);
            txtGia.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[6].Text);
            txtMoTa.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[7].Text);
            txtTrangThai.Text = HttpUtility.HtmlDecode((string)GridView1.Rows[row].Cells[8].Text);
        }
    }
}